package com.shameit.goodqrinfomanager.source.http;


import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;


import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.bean.litepal.ChanPin;
import com.shameit.goodqrinfomanager.bean.litepal.JingXiaoShang;
import com.shameit.goodqrinfomanager.bean.litepal.LoginUser;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import okhttp3.RequestBody;


/**
 * MVVM的Model层，统一模块的数据仓库，包含网络数据和本地数据（一个应用可以有多个Repositor）
 * Created by goldze on 2019/3/26.
 */
public class DataRepository extends BaseModel implements HttpDataSource, LocalDataSource {
    private volatile static DataRepository INSTANCE = null;
    private final HttpDataSource mHttpDataSource;

    private final LocalDataSource mLocalDataSource;

    private DataRepository(@NonNull HttpDataSource httpDataSource,
                           @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static DataRepository getInstance(HttpDataSource httpDataSource,
                                             LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (DataRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DataRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }


    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }

    @Override
    public LoginUser getLoginUser() {
        return mLocalDataSource.getLoginUser();
    }

    @Override
    public void saveLoginUser(String userJson) {
        mLocalDataSource.saveLoginUser(userJson);
    }


    @Override
    public Observable<BaseResponse<LoginUser>> login(RequestBody requestBody) {
        return mHttpDataSource.login(requestBody);
    }

    @Override
    public Observable<BaseResponse<List<CangKu>>> syncCangku(RequestBody requestBody) {
        return mHttpDataSource.syncCangku(requestBody);
    }

    @Override
    public Observable<BaseResponse<List<CangWei>>> syncCangwei() {
        return mHttpDataSource.syncCangwei();
    }

    @Override
    public Observable<BaseResponse<List<JingXiaoShang>>> syncJxs() {
        return mHttpDataSource.syncJxs();
    }

    @Override
    public Observable<BaseResponse<List<ChanPin>>> syncChanpin() {
        return mHttpDataSource.syncChanpin();
    }

    @Override
    public Observable<BaseResponse<Boolean>> uploadBarBind(RequestBody requestBody) {
        return mHttpDataSource.uploadBarBind(requestBody);
    }
}
